package com.wanmait.happyheating.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.happyheating.pojo.Admin;
import com.wanmait.happyheating.pojo.AdminLoginRecord;
import com.wanmait.happyheating.service.AdminLoginRecordService;
import com.wanmait.happyheating.service.AdminService;
import com.wanmait.happyheating.util.PictureUtils;
import com.wanmait.happyheating.util.Result;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**管理员个人信息的控制器
 * @author 于谦涛
 * @since 2021-05-27
 */
@RestController
@RequestMapping("manage")
public class AdminInfoController {
    @Resource
    private AdminService adminService;
    @Resource
    private AdminLoginRecordService adminLoginRecordService;


    /**通过id获得管理员信息
     * @author 于谦涛
     * @since 2021-05-27
     */
    @GetMapping("adminInfo")
    public Result adminInfo(Integer id){
        Admin admin=adminService.getAdmin(id);
        return Result.success("查找成功",admin);
    }
    /**通过id获得管理员登录日志
     * @author 于谦涛
     * @since 2021-05-27
     */
    @GetMapping("adminLoginRecord")
    public Result adminLoginRecord(Integer id,Integer pageNum,Integer pageSize)
    {
        if ("".equals(pageNum)||null==pageNum){
            pageNum=1;
        }
        if ("".equals(pageSize)||null==pageSize){
            pageSize=10;
        }
        PageHelper.startPage(pageNum,pageSize);
        List<AdminLoginRecord> records=adminLoginRecordService.findById(id);
        PageInfo<AdminLoginRecord> recordPageInfo=new PageInfo<>(records);
        return Result.success("获取登录日志成功",recordPageInfo);
    }

    /**修改个人信息
     * @author 于谦涛
     * @since 2021-05-27
     */
    @PostMapping("updateAdminInfo")
    public Result updateAdminInfo(@RequestBody Admin admin){
        System.out.println(admin.getUserName());
        adminService.updateById(admin);
        return Result.success("成功",admin);
    }
    /**更换头像
     * @author 于谦涛
     * @since 2021-05-27
     */
    @PostMapping("upload")
    public void upload(@RequestParam MultipartFile file,Integer id){
        Admin admin=new Admin();
        admin.setId(id);
        //将图片保存到本地并获得图片名
        String profile= PictureUtils.picture(file);
        admin.setProfile(profile);
        adminService.updateById(admin);
        System.out.println(profile);
    }
}
